package cn.livingCloud.service.impl.operation;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.dto.CaseBaseDto;
import cn.livingCloud.entity.core.Case;
import cn.livingCloud.repository.CaseMapper;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Created by 唐旭 on 2016/6/2.
 */
public class CaseBaseDtoLoader<T extends CaseBaseDto> {
    protected String uuid;
    protected transient CaseMapper caseMapper = BeanProvider.getBean(CaseMapper.class);

    public CaseBaseDtoLoader(String uuid) {
        this.uuid = uuid;
    }

    public T load(Class<T> tClass) {
        T result = null;
        try {
            Constructor<T> constructor = tClass.getConstructor(Case.class);
            Case aCase = caseMapper.selectByPrimaryKey(uuid);
            if (aCase != null) {
                result = constructor.newInstance(aCase);
            }
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
            throw new RuntimeException(e);
        }

        return result;
    }


}
